﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace OPX_FlowGlueMachine
{
    public class SkinSetting
    {
        
        [CategoryAttribute("UI设定")]
        public string UIStyle
        {
            get;
            set;
        }
        [CategoryAttribute("UI设定")]
        public string UIPaletteName
        {
            get;
            set;
        }
        [CategoryAttribute("UI设定")]
        public int CheckIndex
        {
            get;
            set;
        }
        [CategoryAttribute("UI设定")]
        public string MachineLanguage
        {
            get;
            set;
        }
        public SkinSetting()
        {
            UIPaletteName = "";
            UIStyle = "DevExpress Style";
            MachineLanguage = "zh-CN";
        }
        public static SkinSetting Load()
        {
            SkinSetting pDoc;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(SkinSetting));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/SkinSetting" + ".xml");
                pDoc = (SkinSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
            }
            catch (IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new SkinSetting();
            }
            catch (Exception eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }

                MessageBox.Show(eMy.ToString() + "即将打开备份文件", Application.StartupPath + @"/MuduleParameter/SkinSetting" + ".xml");
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/SkinSetting" + ".xml.bak");
                pDoc = (SkinSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.Save();
            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(Application.StartupPath + @"/MuduleParameter/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/MuduleParameter/");
            }
            FileStream fsWriter = new FileStream(Application.StartupPath + @"/MuduleParameter/SkinSetting" + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(SkinSetting));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();


            return true;
        }
    }
}
